﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.PostCrawler.Model;
using System.Net;

namespace CarolLib.PostCrawler
{
    public partial class frmMain : Form
    {
        string url_format = "http://184.154.128.246/thread0806.php?fid={0}&search=&type={1}&page={2}";
        string url_format2 = "http://184.154.128.246/thread0806.php?fid={0}&search=&page={1}";

        public frmMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var threads = new TThread().GetList(string.Empty);
            var types = new TType().GetList(string.Empty);

            foreach (var t in threads)
            {
                if (t.ThreadNo < 21)
                {
                    continue;
                }

                var sub = types.Where(tp => tp.ThreadID == t.ThreadID).ToList();


                if (sub.Count == 0)
                {//one sub
                    int page = GetPageCount(t.ThreadNo, 0);

                    for (int i = 1; i < page; i++)
                    {
                        SaveIndex(t, null, i);
                    }
                }
                else
                {//muti sub
                    foreach (var s in sub)
                    {
                        //MessageBox.Show(string.Format(url_format, t.ThreadNo, s.TypeNo, i));

                        int page = GetPageCount(t.ThreadNo, s.TypeNo);

                        for (int i = 1; i < page; i++)
                        {
                            SaveIndex(t, s, i);
                        }
                    }
                }
            }
        }

        private int GetPageCount(int t, int s)
        {
            string content = "";
            if (s == 0)
            {
                content = GetWeb(string.Format(url_format2, t, 1));
            }
            else
            {
                content = GetWeb(string.Format(url_format, t, s, 1));
            }
            // Pages: ( 2/110 total )
            int page = content.IndexOf(" Pages: ( 1/");

            if (page == -1)
            {
                return 1;
            }

            string strpage = content.Substring(page + 12, 20);
            int pageend = strpage.IndexOf(" ");
            int pgcount = int.Parse(strpage.Substring(0, pageend));

            if (pgcount > 100)
            {
                pgcount = 100;
            }

            return pgcount;
        }

        private void SaveIndex(TThread t, TType s, int p)
        {

            List<TPost> result = new List<TPost>();
            string content = "";

            if (s == null)
            {
                content = GetWeb(string.Format(url_format2, t.ThreadNo, p));
            }
            else
                content = GetWeb(string.Format(url_format, t.ThreadNo, s.TypeNo, p));

            //<h3><a href="htm_data/20/1209/803428.html" target="_blank" id="">四季情缘</a></h3>

            string[] start = { "<h3><a href=\"htm_data" };

            var temps = content.Split(start, StringSplitOptions.RemoveEmptyEntries);

            foreach (var temp in temps)
            {
                if (temp.StartsWith("/"))
                {
                    string tt = string.Empty;

                    try
                    {
                        try
                        {
                            tt = temp.Substring(0, 700);
                        }
                        catch
                        {
                            tt = temp.Substring(0, 600);
                        }

                        string url = "htm_data" + tt.Substring(0, tt.IndexOf("\""));
                        string title = string.Empty;
                        string id = string.Empty;
                        string time = string.Empty;

                        if (tt.IndexOf("><font") < 80)
                        {
                            tt = tt.Replace("><font", string.Empty);
                        }

                        title = tt.Substring(tt.IndexOf(">"));
                        title = title.Substring(1, title.IndexOf("<") - 1);

                        id = url.Substring(17, 6);
                        try
                        {
                            time = tt.Substring(tt.IndexOf("<div class=\"f10\">") + 17, 10);
                            time = time.Replace("<div class=\"f10\">", string.Empty);
                        }
                        catch
                        {
                            time = "2012-11-29";
                        }

                        var model = new TPost();
                        model.PostTitle = title;
                        model.Url = url;
                        id = id.Replace(".", string.Empty);
                        model.PostNo = int.Parse(id);
                        model.ThreadID = t.ThreadID;
                        if (s != null)
                            model.TypeID = s.TypeID;
                        else
                            model.TypeID = Guid.Empty;
                        try
                        {
                            model.PostTime = Convert.ToDateTime(time);
                        }
                        catch
                        {
                            model.PostTime = DateTime.Today;
                        }
                        result.Add(model);
                    }
                    catch (Exception e)
                    {
                        //MessageBox.Show(string.Format("Error:{0}-{1}-{2}\n{3}", t.ThreadNo, s.TypeNo, p, e.Message));

                        this.textBox1.Text += "\n";

                        this.textBox1.Text += string.Format("Error:{0}-{1}-{2}\n{3}", t.ThreadNo, s.TypeNo, p, e.Message);
                    }
                }
            }

            foreach (var model in result)
            {
                if (!model.Exists(model.PostNo))
                {
                    model.Add();
                }
            }

            return;
        }

        public static string GetWeb(string url)
        {
            try
            {
                HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);

                wReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11";
                wReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                //wReq.CookieContainer.("Cookie","227c9_lastfid=20; 227c9_lastvisit=0%091354189452%09%2Fthread0806.php%3Ffid%3D20%26search%3D%26type%3D1%26page%3D1; CNZZDATA950900=cnzz_eid=99061666-1354184112-http%253A%252F%252F184.154.128.246%252F&ntime=1354184112&cnzz_a=83&retime=1354189249822&sin=&ltime=1354189249822&rtime=0");

                // Get the response instance.
                System.Net.WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();
                // Dim reader As StreamReader = New StreamReader(respStream)
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding("GBK")))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (System.Exception ex)
            {
                //errorMsg = ex.Message;
            }
            return "";
        }
    }
}
